package com.edu.ujn.myh.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * @作者 马永华
 * @日期 2010-4-15下午04:03:39
 * @版本 1.0.0
 */
public class HQL {

	/**
	 * 根据提供的名值对生成sql 查询条件
	 * 
	 * @param condition
	 * @param hql
	 * @param flag
	 * @return
	 */
	public static String hql(Map condition, String hql, String flag) {

		Set set = condition.keySet();
		int num = 0;
		if (condition.size() != 0) {
			for (Iterator iter = set.iterator(); iter.hasNext();) {

				String key = (String) iter.next();
				if (!"".equals(condition.get(key))
						&& condition.get(key) != null) {
					if (num == 0) {
						hql = hql + " where " + flag + "." + key + " like "
								+ "'%" + condition.get(key) + "%'";

					} else {
						hql = hql + " and " + flag + "." + key + " like "
								+ "'%" + condition.get(key) + "%'";
					}
					num++;
				}
			}
		}
		return hql;
	}

	public static String sql(String sql, List list, String flag) {
		int num = 0;
		for (int i = 0; i < list.size(); i++) {
			if (num == 0) {
				sql = sql + " and (" + flag + " = " + list.get(i);
			} else {
				sql = sql + " or " + flag + " = " + list.get(i);
			}
			num++;
		}
		return sql = sql + " ) order by priv.p_index";
	}
}
